<template>
	<view>
		

		<view class="" style="width: 200rpx;margin: 100rpx auto 0  auto;">
			<u-image src="/static/logo.png" width="200" height="200" mode="widthFix"></u-image>
		</view>
		<view
			style="width: 500rpx;text-align: center;color: #333;margin: 40rpx auto;font-size: 34rpx;font-weight: bold;">
			幸福上门请求获取您的微信授权</view>

		<u-button shape="circle"
			:custom-style="{background:'#628DFF',color:'#fff',marginTop:'40rpx',position: 'fixed',left:'125rpx',bottom:'100rpx',width:'500rpx'}"
			@click="appLoginWx" v-if="isregister">
			微信一键登录</u-button>

		<u-button shape="circle" open-type="getPhoneNumber"
			:custom-style="{background:'#628DFF',color:'#fff',marginTop:'40rpx',position: 'fixed',left:'125rpx',bottom:'100rpx',width:'500rpx'}"
			@getphonenumber="getphonenumber" v-else>
			手机号注册</u-button>


		<view style="position: fixed;width: 200rpx;text-align: center;bottom: 40rpx;left:275rpx;color: #999999;"
			@click="close">暂时跳过
		</view>


		<u-toast ref="uToast" />


	</view>
</template>

<script>
	export default {
		data() {
			return {
				isregister: true,
				iv1: "",
				encryptedData1: "",
				userInfo:''
			}
		},
		onLoad(data) {
			if (data.code) {
				 this.$u.post('/user/mpLogin', {
					 code: data.code,
				 }).then(res => {
				    this.afterLogin(res)
				 }).catch(err => {
					   
				 })
			}
		},
		methods: {
			close() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			appLoginWx() {

				uni.showLoading({
					mask: true
				})
				
				// #ifdef MP-WEIXIN
				   uni.getUserProfile({
				   		desc: "Wexin",
				   		success: rese => {
				   			let avatar=rese.userInfo.avatarUrl;
				   			let nickName=rese.userInfo.nickName;
				   			let gender=rese.userInfo.gender;
				   			
				   			uni.login({
				   				success: login => {
				   					this.$u.post('/user/xcxLogin', {
				   						"avatar":avatar,
				   						"code": login.code,
				   						"encryptedData": rese.encryptedData,
				   						"gender":gender,
				   						"iv": rese.iv,		
				   						"pid": uni.getStorageSync("pid")
				   					}).then(res => {
				   						  
				   						if (!res.mobile ) {
				   							
				   							this.isregister = false;
				   							
				   							this.userInfo=res;
				   						// 	this.iv1 = rese.iv
				   						
				   						// 	this.encryptedData1 = rese.encryptedData
				   						} else {
				   							this.afterLogin(res)
				   						}
				   					}).catch(err => {
				                          
				   					})
				   				},
				   				
				   
				   			})
				   		}			
				   })
				// #endif
				
				// #ifdef H5
						this.$u.post('/user/mpLoginUrl', {
						   url:'https://sm.soft.haoyangsoft.cn/pages/register/index'
						}).then(res => {
							  window.location.href = res;
						}).catch(err => {
							  
						})						
				// #endif
				

				
			},
			getphonenumber(e) {

				if (e.detail.errMsg == 'getPhoneNumber:ok') {

					uni.login({
						success: (res) => {

							var {
								encryptedData,
								iv
							} = e.detail
							this.$u.post('/index/getPhone', {
								"code": res.code,
								"encryptedData": encryptedData,
								"iv": iv,
							}).then(res => {
								
								this.userInfo.mobile=res;
								this.afterLogin(this.userInfo)



							}).catch(err => {

							})

						},
					})

				} else {
					uni.showModal({
						content: '检测到您还没有打开授权手机号，是否去设置打开？',
						confirmText: "确认",
						cancelText: "取消",
						success: function(res) {
							if (res.confirm) {
								uni.openSetting({
									success: (res) => {

									}
								})
							} else {
								console.log('用户点击取消')
							}
						}
					})
				}
			},
			afterLogin(res) {
				
				this.$store.commit("SET_USERINFO",res)
				this.$refs.uToast.show({
					title: '登录成功',
					type: 'success',
				})
				let tabs = ["/pages/index/index", "/pages/shop/index", "/pages/car/index", "/pages/my/index"];
				if (this.$store.state.currentPagePath) {
					if (tabs.filter(item => item == this.$store.state.currentPagePath)) {
						uni.switchTab({
							url: this.$store.state.currentPagePath
						})
					}
					uni.redirectTo({
						url: this.$store.state.currentPagePath + this.$u.queryParams(this.$store.state
							.currentPageOption)
					})
				} else {
					if (getCurrentPages().length == 1) {
						uni.switchTab({
							url: "/pages/index/index"
						})
					} else {
						uni.navigateBack({

						})
					}
				}

			}



		}
	}
</script>

<style>
	.Topbg {
		height: 300rpx;
		background: #628DFF;

		border-radius: 0rpx 0rpx 50% 50%;
		display: inline-block;
		width: 750rpx;
	}

	.line {
		width: 100rpx;
		height: 14rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: auto;
	}
</style>
